class_name StateMachine

extends Node

const KEEP_CURRENT := -1

# 更新当前状态
var current_state: int = -1:
	set(v):
		# 执行根节点的状态转换逻辑
		owner.transition_state(current_state, v)
		#print("player status from :", current_state, " to: ", v)
		current_state = v
		state_time = 0
		
var state_time: float

# 初始化当前状态为第一个状态
func _ready() -> void:
	await owner.ready
	current_state = 0

# 每帧执行
func _physics_process(delta: float) -> void:
	# 循环更新到下一状态，直到状态不变则退出
	while true:
		# 获取下一状态
		var nexet_state = owner.get_next_state(current_state) as int
		if nexet_state == KEEP_CURRENT:
			break
		else:
			current_state = nexet_state
	# 执行根节点的动作渲染方法
	owner.tick_physics(current_state, delta)
	# 记录每个状态持续时间
	state_time += delta
